let city = ''
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})
const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      rules: {
        Trading_Type: [{ required: true, message: '请选择打款方式', trigger: 'change' }],
      },
      form: {
      },//表单数据
      formData: {
        Group_Seek_Transaction_SerialNumber: "", //主键
        Group_Seek_Purchase_SerialNumber: "", //流水号
        Quantity: "", //总量
        Amount: "", //钱
        Submission_Time: "", //时间
        Shipment_SerialNumber: "",
        Transport_SerialNumber: "",
        Payment_SerialNumber: "",
        Reservation_Time: "",
        Seller_Contact: "", //买主姓名
        Seller_Contact_Phone: "", //买主手机号
        Seller_Enterprise_Info_SerialNumber: "", //买主公司编号
        Enterprise_Info_SerialNumber: "", //卖家公司编号
        Children: {
          group_seek_transaction_paystages: [{
            Group_Seek_Paystages_SerialNumber: "",
            Group_Seek_Transaction_SerialNumber: "",
            Trading_Name: "",
            Trading_Amount: "",
            Trading_Date: "",
            Amount: "",
            Collection_Amount: ""
          }]
        }
      },
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      value: '',
      Trading_Type: [],//打款方式
      Shipment_Type: [],//装运方式
      Transport_Type: [],//运输方式
      Payment_Type: [],//付款方式
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      loading: false,
      num: 1,
    }
  },
  mounted() {
    const Group_Seek_Purchase_SerialNumber = sessionStorage.getItem('Group_Seek_Purchase_SerialNumber')
    console.log(Group_Seek_Purchase_SerialNumber)
    this.getData(Group_Seek_Purchase_SerialNumber)
  },
  methods: {
    //获取上一页传来的数据
    getData(Group_Seek_Purchase_SerialNumber) {
      let _this = this;
      $.ajax({
        url: DomainName + '/getGroupSeekProcureIDAction',
        type: 'POST',
        data: {
          Group_Seek_Purchase_SerialNumber,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.form = res.Data
          console.log(res)
          _this.Trading_Type = res.Paystages
          _this.Shipment_Type = res.Shipment
          _this.Transport_Type = res.Transport
          _this.Payment_Type = res.Payment
          _this.num = res.Data.Starting_Quantity
          console.log(_this.num)
        }
      })
    },
    // 生成日志
    submitForm() {
      let year = new Date().getFullYear();
      let month = new Date().getMonth() + 1;
      let days = new Date().getDate();
      let hour = new Date().getHours()
      let minutes = new Date().getMinutes()
      let second = new Date().getSeconds()
      if (month < 10) { month = '0' + month }
      if (days < 10) { days = '0' + days }
      if (hour < 10) { hour = '0' + hour }
      if (minutes < 10) { minutes = '0' + minutes }
      if (second < 10) { second = '0' + second }
      let times = year + '-' + month + '-' + days + ' ' + hour + ':' + minutes + ':' + second
      this.formData.Submission_Time = times
      this.formData.Enterprise_Info_SerialNumber = this.form.Enterprise_Info_SerialNumber
      this.formData.Group_Seek_Purchase_SerialNumber = this.form.Group_Seek_Purchase_SerialNumber
      this.formData.Quantity = this.num
      this.formData.Amount = this.form.Amount
      this.formData.Seller_Enterprise_Info_SerialNumber = this.form.Enterprise_Info_SerialNumber
      if (this.form.Trading_Name == '按阶段付款') {
        this.formData.Children.group_seek_transaction_paystages = []
        this.Trading_Type.forEach(item => {
          this.formData.Children.group_seek_transaction_paystages.push({
            Group_Seek_Paystages_SerialNumber: "",
            Group_Seek_Transaction_SerialNumber: "",
            Trading_Detail_SerialNumber: item.Trading_Detail_SerialNumber,
            Trading_Name: item.Trading_Detail_Name,
            Trading_Explain: item.Trading_Explain,
            Trading_Amount: item.Trading_Amount,
            Trading_Date: item.Trading_Date,
            Amount: item.Trading_Amount * this.num * this.form.Amount / 100,
            Collection_Date: "",
            Collection_Amount: 0
          })
        })
      } else {
        this.formData.Children.group_seek_transaction_paystages = [{
          Group_Seek_Paystages_SerialNumber: "",
          Group_Seek_Transaction_SerialNumber: "",
          Trading_Detail_SerialNumber: this.Trading_Type[0].Trading_Detail_SerialNumber,
          Trading_Name: this.Trading_Type[0].Trading_Detail_Name,
          Trading_Explain: this.Trading_Type[0].Trading_Explain,
          Trading_Amount: this.Trading_Type[0].Trading_Amount,
          Trading_Date: this.Trading_Type[0].Trading_Date,
          Amount: this.Trading_Type[0].Trading_Amount * this.num * this.form.Amount / 100,
          Collection_Date: "",
          Collection_Amount: 0
        }]
      }
      let reg = /^1[3-9]\d{9}$/
      if (this.formData.Buyer_Contact == '') {
        this.$message({
          message: '请输入联系人',
          type: 'warning'
        });
        return
      }
      if (this.formData.Seller_Contact == '') {
        this.$message({
          message: '请输入联系人手机号',
          type: 'warning'
        });
        return
      }
      if (!reg.test(this.formData.Seller_Contact_Phone)) {
        this.$message({
          message: '手机号输入有误,请仔细检查',
          type: 'warning'
        });
        return
      }
      if (this.formData.Trading_Type == '') {
        this.$message({
          message: '请选择打款方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Shipment_Type == '') {
        this.$message({
          message: '请选择装运方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Transport_Type == '') {
        this.$message({
          message: '请选择运输方式',
          type: 'warning'
        });
        return
      }
      if (this.formData.Payment_Type == '') {
        this.$message({
          message: '请选择付款方式',
          type: 'warning'
        });
        return
      }
      let _this = this;
      $.ajax({
        url: DomainName + '/getAddAction',
        type: 'POST',
        data: {
          Data: JSON.stringify({ "group_seek_transaction": [_this.formData] }),
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          var group_seek_message = {
            Group_Seek_Message_SerialNumbel: "",
            Group_Seek_Transaction_SerialNumber: res.Data,
            Cause: "提交成功",
            Submission_Time: times,
            Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            Seller_Type: "0"
          }
          var qroup_seek_purchase = {
            Group_Seek_Message_SerialNumbel: "",
            Group_Seek_Transaction_SerialNumber: res.Data,
            Cause: "申请提示",
            Submission_Time: times,
            Enterprise_Info_SerialNumber: _this.form.Enterprise_Info_SerialNumber,
            Seller_Type: "1"
          }
          var group_message = []
          group_message.push(group_seek_message)
          group_message.push(qroup_seek_purchase)
          console.log(group_message)
          _this.submitMessage(group_message)
        }
      })
    },
    // 全部求购检验库存
    Inventory(Group_Seek_Purchase_SerialNumber) {
      let _this = this
      _this.loading = true
      console.log(Group_Seek_Purchase_SerialNumber, _this.num)
      $.ajax({
        url: DomainName + '/GroupSeekCheckStockAction',
        type: 'POST',
        data: {
          Group_Seek_Purchase_SerialNumber,
          Quantity: _this.num,
          token: $.cookie("token")
        },
        success: function (res) {
          console.log(res)
          if (res.Status == 'success') {
            _this.submitForm()
          } else {
            _this.$notify.error({
              title: '错误',
              message: '库存不足'
            });
          }
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
      })
    },
    // 提交表单
    submitMessage(data) {
      var _this = this
      $.ajax({
        url: DomainName + '/getAddAction',
        type: 'POST',
        data: {
          Data: JSON.stringify({ "group_seek_message": data }),
          token: $.cookie("token")
        },
        success: function (res) {
          // _this.loading = false
          console.log(res)
          if (res.Status == 'success') {
            _this.$notify({
              title: '成功',
              message: '对方企业审核中,请等待',
              type: 'success'
            });
            window.history.go(-1);
          } else {
            _this.$notify.error({
              title: '错误',
              message: '系统错误..'
            });
          }
        }
      })
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      if (event) {
        console.log(event)
        this.isdisabled = false
      } else {
        this.isdisabled = true
      }
    },
    selectAddress(event) {
      if (event && event != '' && this.selectArea.label != undefined) {
        this.form.Supplier_Address = this.selectProvince.label + '/'
          + this.selectCity.label + '/'
          + this.selectArea.label + '/'
          + this.address
      }
    },
  }
})